<?php
/**
* @version $Id: jceutilities.php 2008-01-07 $
* @package JCEUtilites
* @copyright Copyright (C) 2006/2007/2008 Ryan Demmer. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
* This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
*/

// no direct access
defined( '_VALID_MOS' ) or die( 'Restricted access' );

$_MAMBOTS->registerFunction( 'onAfterStart', 'botJceUtilities' );

class JceUtilities { 
	function renderParams( $name, $params, $end ){
		$html = '';
		if( $name ){
			$html .= "'". $name ."':{";
		}
		$i = 0;
		foreach( $params as $k => $v ){
			if( !is_numeric( $v ) ){
				$v = '"'. $v .'"';
			}
			if( $i < count( $params ) -1 ){
				$v .= ',';
			}
			$html .= "'". $k ."':". $v;
			$i++;
		}
		if( $name ){
			$html .= "}";
		}
		if( !$end ){
			$html .= ",";
		}
		return $html;
	}
	/** 
	 * Borrowed from libraries/joomla/environment/uri.php in Joomla! 1.5.7
	 * @copyright	Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
	 * @license		GNU/GPL, see LICENSE.php
	 * Joomla! is free software. This version may have been modified pursuant
	 * to the GNU General Public License, and as distributed it includes or
	 * is derivative of works licensed under the GNU General Public License or
	 * other free or open source software licenses.
	 */
	function getURI(){
		if (strpos(php_sapi_name(), 'cgi') !== false && !empty($_SERVER['REQUEST_URI'])) {
			//Apache CGI
			return  rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
		} else {
			//Others
			return  rtrim(dirname($_SERVER['SCRIPT_NAME']), '/\\');
		}
	}
	function renderOutput(){
		global $mainframe, $database;
	
		$pop = intval( mosGetParam( $_REQUEST, 'pop', 0 ) );
		$task = mosGetParam( $_REQUEST, 'task' );
		
		if( $pop || ( $task == 'new' || $task == 'edit' ) ){
			return;
		}
		
		$version = '217';
			
		$query = "SELECT id, published"
		. "\n FROM #__mambots"
		. "\n WHERE element = 'jceutilities'"
		. "\n AND folder = 'system'"
		;
		$database->setQuery( $query );
		$id = $database->loadResult();
		$mambot = new mosMambot( $database );
		$mambot->load( $id );
		$params = new mosParameters( $mambot->params );
		
		if( !$mambot->published ) return;
		
		$components = $params->get('components', '');
		if( $components ){
			$excluded 	= explode( ',', $components );
			$option 	= mosGetParam( $_REQUEST, 'option' );
			foreach( $excluded as $exclude ){
				if( $option == 'com_'. $exclude || $option == $exclude ){
					return;
				}
			}
		}
		
		$query = "SELECT published"
		. "\n FROM #__mambots"
		. "\n WHERE element = 'jceembed'"
		. "\n AND folder = 'system'"
		;
		$database->setQuery( $query );
		$embed = $database->loadResult();
		
		$popup = array(
			'legacy'			=>	$params->get( 'legacy', '0' ),
			//'convert'			=>	$params->get( 'convert', '0' ),
			'resize'			=>	$params->get( 'resize', '1' ),
			'icons'				=>	$params->get( 'icons', '1' ),
			'overlay'			=>	$params->get( 'overlay', '1' ),
			'overlayopacity'	=>	$params->get( 'overlayopacity', '0.8' ),
			'overlaycolor'		=>	$params->get( 'overlaycolor', '#000000' ),
			'width'				=>	$params->get( 'width', '640' ),
			'height'			=>	$params->get( 'height', '480' ),
			'hideobjects'		=>	$params->get( 'hideobjects', '1' ),
			'fadespeed'			=>	$params->get( 'fadespeed', '500' ),
			'scalespeed'		=>	$params->get( 'scalespeed', '500' ),
			'theme'				=>	$params->get( 'theme', 'standard' ),
			'themecustom'		=>	$params->get( 'themecustom', '' ),
			'themepath'			=>	$params->get( 'themepath', 'mambots/system/jceutilities/themes' ),
		);
		$tooltip = array(
			'classname'			=>	$params->get( 'tipclass', 'tooltip' ),
			'opacity'			=>	$params->get( 'tipopacity', '1' ),
			'speed'				=>	$params->get( 'tipspeed', '150' ),
			'position'			=>	$params->get( 'tipposition', 'br' ),
			'offsets'			=>	"{'x': ". $params->get( 'tipoffsets_x', '16' ) .", 'y': ". $params->get( 'tipoffsets_y', '16' ) ."}"
		);
		$standard = array(
			'imgpath'			=>	$params->get( 'imgpath', 'mambots/system/jceutilities/img' ),
			'pngfix'			=>	$params->get( 'pngfix', '0' ),
			'wmode'				=>	$params->get( 'wmode', '0' )
		);
		
		$versions = array(
				'flash'			=>	$params->get( 'flash', '9,0,124,0' ),
				'windowmedia'	=>	$params->get( 'windowmedia', '5,1,52,701' ),
				'quicktime'		=>	$params->get( 'quicktime', '6,0,2,0' ),
				'realmedia'		=>	$params->get( 'realmedia', '7,0,0,0' ),
				'shockwave'		=>	$params->get( 'shockwave', '8,5,1,0' )
			);
		
		$base 	= JceUtilities::getURI();
		$theme 	= $popup['theme'] == 'custom' ? $popup['themecustom'] : $popup['theme'];
		$html = '';
		
		$html .= "<script type=\"text/javascript\" src=\"" . $base . "/mambots/system/jceutilities/js/jquery-126.js\"></script>\n";
		$html .= "<script type=\"text/javascript\" src=\"" . $base . "/mambots/system/jceutilities/js/jceutilities-".$version.".js\"></script>\n";
		
		if( !$embed ){
			$html .= "<script type=\"text/javascript\" src=\"" . $base . "/mambots/system/jceutilities/js/mediaobject.js\"></script>\n";
		}
		$html .= "<link href=\"" . $base . "/mambots/system/jceutilities/css/jceutilities-".$version.".css\" rel=\"stylesheet\" type=\"text/css\" media=\"all\"/>\n";
		
		// Load template css file
		if( file_exists( $mainframe->getCfg('absolute_path') . DIRECTORY_SEPARATOR . $popup['themepath'] . DIRECTORY_SEPARATOR . $popup['theme']. '/css/style.css' ) ){
			$html .= "<link href=\"" . $base . "/" . $popup['themepath'] . "/" . $popup['theme'] ."/css/style.css\" rel=\"stylesheet\" type=\"text/css\" media=\"all\"/>\n";
		}
		// Load any ie6 variation
		$browser = @get_browser();
		if( $browser ){
			if( preg_match( '/msie/i', $browser->browser ) && $browser->majorver < 7 ){
				if( file_exists( $mainframe->getCfg('absolute_path') . DIRECTORY_SEPARATOR . $popup['themepath'] . DIRECTORY_SEPARATOR . $popup['theme']. '/css/style_ie6.css' ) ){
					$html .= "<link href=\"" . $base . "/" . $popup['themepath'] . "/" . $popup['theme'] ."/css/style_ie6.css\" rel=\"stylesheet\" type=\"text/css\" media=\"all\"/>\n";
				}
			}
		}		
		$html .= "<script type=\"text/javascript\">";
		if( !$embed ){
			$html .= "MediaObject.init({";
			$html .= JceUtilities::renderParams( '', $versions, true );
			$html .= "});";
		}
		$html .= "jQuery(document).ready(function(){jceutilities({";
		$html .= JceUtilities::renderParams( 'popup', $popup, false );
		$html .= JceUtilities::renderParams( 'tootlip', $tooltip, false );
		$html .= JceUtilities::renderParams( '', $standard, true );
		$html .= "});});</script>";
						
		$mainframe->addCustomHeadTag( $html );
		return true;
	}
}
function botJceUtilities(){
	JceUtilities::renderOutput();
}
?>